package com.sky.controller.user;

import com.google.gson.annotations.Until;
import com.sky.entity.*;
import com.sky.mapper.*;
import com.sky.result.Result;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.el.stream.Stream;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.integration.IntegrationProperties;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/user")
@RestController
@Slf4j
@ApiModel
/**
 * 用户商品浏览
 */
public class UserThroughController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DisFlavorMapper disFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private dishMapper dishMapper;
    @Autowired
    private setMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 查询分类包括套餐和菜品
     * @param type
     * @return
     */
    @GetMapping("/category/list")
    public Result categorySelect(String type){
        List<Category> select = categoryMapper.select(type);
        return Result.success(select);
    }
    /*@GetMapping("/hello")
    @CrossOrigin(origins = "http://localhost:63342")  // 允许来自 http://localhost:63342 的请求
        public Result hello(){
        log.info("hello");
        return Result.success("hello");
        }*/

    /**
     * 查询菜品里面包含的菜
     * @param categoryId
     * @return
     */
    @GetMapping("/dish/list")
    public Result dishSelect(String categoryId){
        //取出该redis缓存中的键对应的值
        String key="dish_"+categoryId;
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        //判断值是否存在
        if(list==null||list.size()==0){
            //不存在，读取数据库
            List<Dish> dishes = dishMapper.selectByCategoryId(Integer.parseInt(categoryId));
            list=new ArrayList<>();
            for (Dish dish : dishes) {
                DishVO dishVO=new DishVO();
                Long id = dish.getId();
                List<DishFlavor> dishFlavors = disFlavorMapper.selectByDishId(Math.toIntExact(id));
                BeanUtils.copyProperties(dish,dishVO);
                dishVO.setFlavors(dishFlavors);
                list.add(dishVO);
            }
            //将读取的数据存入缓存
            redisTemplate.opsForValue().set(key,list);
        }
        return Result.success(list);
    }

    /**
     * 查询套餐
     * @param categoryId
     * @return
     */
    @GetMapping("/setmeal/list")
    @Cacheable(cacheNames = "setMealCache",key="#categoryId")
    public Result setmealSelect(String categoryId){
        List<Setmeal> setmeal =  setMealMapper.cheakByCategoryId(categoryId);
        List<Setmeal> collect = setmeal.stream().filter(s -> s.getStatus()== 1).collect(Collectors.toList());
        return Result.success(collect);
    }

    /**
     * 查询套餐餐品信息
     * @param id
     * @return
     */
    @GetMapping("/setmeal/dish/{id}")
    public Result selectDishOnSetmeal(@PathVariable String id){
        List<DishItemVO> setmealDishes = setMealDishMapper.selectBySetmealId2(Integer.parseInt(id));
        return Result.success(setmealDishes);
    }


}
